<!DOCTYPE html>
<html class="x-admin-sm">
    <head>
        <meta charset="UTF-8">
        <title>添加</title>
        <meta name="renderer" content="webkit">
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
        <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
        <link rel="stylesheet" href="../css/font.css">
        <link rel="stylesheet" href="../css/xadmin.css">
        <script src="../lib/layui/layui.js" charset="utf-8"></script>
        <script type="text/javascript" src="../js/xadmin.js"></script>
        <script src="../lib/axios.min.js"></script>
        <script src="../lib/layui/layui-expand/enhanceform.js"></script>
        <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
        <!--[if lt IE 9]>
            <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
            <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
        <![endif]-->
    </head>
    <body>
        <div class="layui-fluid">
            <div class="layui-row">
                <form class="layui-form" id="mainForm">
                    <div id="view"></div>
                    <div class="layui-form-item">
                        <label for="L_repass" class="layui-form-label">
                        </label>
                        <button  class="layui-btn" lay-filter="add" lay-submit="">
                            保存
                        </button>
                    </div>
                </form>
            </div>
        </div>
        <script type="text/html" id="demo">
            <div class="layui-row">
                {{# var i = 0; }}
                {{# layui.each(kin, function(index, item){ }}
                    {{# if(item.type == "1" || item.type == "3"){ }}
                    <div class="layui-form-item">
                    {{# }else{ i=i+1; }}
                    {{# if(i%3 == 0 || i == 1){ }}
                    <div class="layui-form-item">
                    {{# } } }}
                    {{# if(item.required){item.required="required" }}
                    <label for="{{item.field}}" class="layui-form-label">
                        <span class="x-red">*</span>{{item.title}}
                    </label>
                    {{# }else{ }}
                    <label for="{{item.field}}" class="layui-form-label">
                        {{item.title}}
                    </label>
                    {{# } }}
                    
                    {{# switch(item.type){
                    case "0":}}
                    <div class="layui-input-inline">
                        <input name="{{item.field}}" class="layui-input" lay-verify="{{item.required}}">
                    </div>
                    {{# break;
                    case "1":}}
                    <div class="layui-input-block">
                        <input name="{{item.field}}" class="layui-input" autocomplete="on" lay-verify="{{item.required}}">
                    </div>
                    {{# break;
                    case "2":}}
                    <div class="layui-input-inline">
                        <select name="{{item.field}}" class="layui-input" lay-verify="{{item.required}}">
                        {{# layui.each(item.oval, function(inde, ite){ }}
                        <option value="{{ite.val}}">{{ite.tit}}</option>
                        {{# }); }}
                        {{#  if(item.oval.length === 0){ }}
                            无数据
                        {{#  } }}
                        </select>
                    </div>
                    {{# break;
                    case "3":}}
                    <div class="layui-input-block">
                        <textarea name="{{item.field}}" class="layui-textarea" lay-verify="{{item.required}}"></textarea>
                    </div>
                    {{# break;
                    default:}}
                    <div class="layui-input-inline">
                        <input name="{{item.field}}" class="layui-input" required lay-verify="required">
                    <div>
                    {{# break;
                        } }}
                
                    {{# if(item.type=="1" || item.type=="3"){ }}
                    </div>
                    {{# }else{ if(i%3 == 2){ }}
                    </div>
                    {{# } } }}

                {{#  }); }}
                {{#  if(kin.length === 0){ }}
                    无数据
                {{#  } }}
            </div>   
        </script>
    </body>
</html>
<script>
    var ki = Number(localStorage.getItem("k"));
    localStorage.removeItem("k");
    var kin;
    axios.post("/api/get_kk", {kid: ki}).then(res=>{
        kin = res.data.head;
        layui.use(['laytpl', 'form'], function(){
            var laytpl = layui.laytpl
            ,form = layui.form
            ,enhanceForm = layui.enhanceform;
           
            var getTpl = demo.innerHTML
            ,view = document.getElementById('view');
        
            var j = [];
            for (var i in kin){
                if (kin[i].type == "2"){
                    j.push({index: i, title: kin[i].field});
                }
            }
            axios.post("/api/get_ko", {oid: ki}).then(res=>{
                var op = res.data;
                for (var k in j){
                    for (var z in op){
                        if(j[k].title == op[z].okey){
                            kin[Number(j[k].index)].oval = op[z].ovalue;
                        }
                    }
                }
                laytpl(getTpl).render(kin, function(html){
                    view.innerHTML = html;
                });
                form.render();
            })
            form.on('submit(add)', function(data) {
                var formData = data.field;
                axios.post('/api/add_d', {kid: ki, body: formData}).then(function(res) {
                    layer.msg("保存成功！");
                    function pad(num, n) {  
                    var len = num.toString().length;  
                    while(len < n) {  
                        num = "0" + num;  
                        len++;  
                    }  
                    return num;  
                }
                var enhance = new enhanceForm({
                    elem: '#mainForm'
                });
                for (var i in kin){
                    switch (kin[i].addto){
                        case "0":
                            formData[kin[i].field] = "";
                            break;
                        case "2":
                            var wei = formData[kin[i].field].length;
                            formData[kin[i].field] = pad(Number(formData[kin[i].field])+1,wei);
                            break;
                    };
                }
                enhance.filling(formData);
                })

                return false;
            });
        })

    })

</script>